package com.anXin.mall.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.anXin.mall.mapper.OrderMapper;
import com.anXin.mall.service.GoodsService;
import com.anXin.mall.service.OrderService;
import com.anXin.model.common.dtos.ResponseResult;
import com.anXin.model.mall.pojos.Goods;
import com.anXin.model.mall.pojos.Order;
import com.anXin.model.mall.vos.OrderVO;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;


import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    @Resource
    private GoodsService goodsService;

    @Override
    public ResponseResult<Page<Order>> getOrdersPage(int page,int pageSize) {
        Page pageInfo = new Page(page,pageSize);
        return ResponseResult.okResult(query().page(pageInfo));
    }

    @Override
    public ResponseResult<List<OrderVO>> getOrdersList(Long userId) {
        List<Order> list = query().eq("user_id", userId).list();
        List<OrderVO> collect = list.stream().map(item -> {
            OrderVO orderVO = BeanUtil.copyProperties(item, OrderVO.class);
            Goods goods = goodsService.getById(item.getGoodsId());
            orderVO.setGoodsName(goods.getTitle());
            orderVO.setGoodsPrice(goods.getPrice());
            return orderVO;
        }).collect(Collectors.toList());

        return ResponseResult.okResult(collect);
    }

    @Override
    public ResponseResult<OrderVO> getOrder(Long id) {
        Order order = query().eq("id", id).one();
        OrderVO orderVO = BeanUtil.copyProperties(order, OrderVO.class);
        Goods goods = goodsService.getById(orderVO.getGoodsId());
        orderVO.setGoodsName(goods.getTitle());
        orderVO.setGoodsPrice(goods.getPrice());
        return ResponseResult.okResult(orderVO);
    }

    @Override
    public ResponseResult<String> updataOrder(Order order) {
        if(order.getId()!=null){
            updateById(order);
        }else {
            goodsService.getGoods(order.getGoodsId());
        }
        return ResponseResult.okResult("添加成功");
    }

    @Override
    public ResponseResult<String> deleteOrder(Long id) {
        removeById(id);
        return null;
    }

}
